/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PIServer;

import Service.AdminService;
import Service.HttpResponseConstant;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.io.OutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Apolloz
 */
public class AdminHttpHandler implements HttpHandler {

    public static final Logger log = LoggerFactory.getLogger(AdminHttpHandler.class.getName());
    private AdminService adminService = new AdminService();

    public void handle(HttpExchange exchange) throws IOException {
        String requestMethod = exchange.getRequestMethod();
        if (requestMethod.equalsIgnoreCase("GET")) {
            String command = exchange.getRequestURI().getPath();
            if (command.length() == 0) {
                return;
            }
            command = command.substring(1); // remove "/"

            Headers requestHeaders = exchange.getRequestHeaders();
            Headers responseHeaders = exchange.getResponseHeaders();
            OutputStream responseBody = exchange.getResponseBody();

//            responseHeaders.set("Content-Type", "text/plain; charset=utf-8");
//            responseHeaders.set("Key", "Value");
//            responseHeaders.set("abc", "xyz");

            int errCode = processCommand(command, 
                    requestHeaders, responseHeaders, responseBody,
                    exchange);

            exchange.sendResponseHeaders(errCode, 0);

//            Headers requestHeaders = exchange.getRequestHeaders();
//            Set<String> keySet = requestHeaders.keySet();
//            Iterator<String> iter = keySet.iterator();
//            while (iter.hasNext()) {
//                String key = iter.next();
//                List values = requestHeaders.get(key);
//                String s = key + " = " + values.toString() + "\n";
//                responseBody.write(s.getBytes());
//            }
//
//            String s = "TEST";
//            responseBody.write(s.getBytes());
            responseBody.close();
        }
    }

    private int processCommand(String command,
            Headers requestHeaders,
            Headers responseHeaders,
            OutputStream responseBody,
            HttpExchange exchange) throws IOException {

        String response = "Request is processed";

        if (command == null) {
            return HttpResponseConstant.HTTP_RESPONSE_INTERNAL_SERVER_ERROR;
        }

        int result = HttpResponseConstant.HTTP_RESPONSE_INTERNAL_SERVER_ERROR;
        
        //KICK USER FUNCTION
        if (command.equals("kickuser")) {
            log.info("Kick user request");
            result = adminService.kickUser(requestHeaders, responseHeaders, responseBody);
        } //BROADCAST ADMIN ANNOUNCEMENT
        else if (command.equals("broadcast")) {
            log.info("Admin Announcement request");
            result = adminService.broadcastAdminMessage(requestHeaders, responseHeaders, responseBody);
        } //SEND USER INFO FOR ADMIN
        else if (command.equals("getinfo")) {
            log.info("Get user info request");
            result = adminService.sendUserInfo(requestHeaders, responseHeaders, responseBody);
        } else if (command.equals("useronline")) {
            log.info("Get users online");
            result = adminService.sendNumberUserOnline(requestHeaders, responseHeaders, responseBody);
        } else if (command.equals("listuseronline")) {
            log.info("Get list users online");
            result = adminService.sendListUserOnline(requestHeaders, responseHeaders, responseBody);
        } else if (command.equals("broadcastmess")) {
            log.info("Get broadcast message");
            result = adminService.sendBroadcastList(requestHeaders, responseHeaders, responseBody);
        }
        else if (command.equals("updatenews")) {
            log.info("GM update news");
            result = adminService.updateNews(requestHeaders, responseHeaders, responseBody);
        }
//        else if (command.equals("restart")) {
//            log.info("Server restart required");
//            result = adminService.restartServer(requestHeaders, responseHeaders, responseBody);
//        }
        else if (command.equals("shutdown")) {
            log.info("Server shutdown required");
            response = "Shutdown process is proceeded...";
            exchange.sendResponseHeaders(result, response.length());
            responseBody.write(response.getBytes());
            responseBody.close();
            result = adminService.shutdownServer(requestHeaders, responseHeaders, responseBody);
        }
        else {
            response = "Unrecognized command";
        }

        // return response message
        exchange.sendResponseHeaders(result, response.length());
        responseBody.write(response.getBytes());
        responseBody.close();

        return result;
    }
}
